package com.shark.open.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shark.model.trade.FuelOrderModel;
import com.shark.model.crm.*;
import com.shark.open.config.ReqContext;
import com.shark.open.criteria.FuelOrderCriteria;
import com.shark.open.mapper.FuelOrderMapper;
import com.shark.open.vo.PageVo;
import com.shark.utils.security.MD5Util;
import com.shark.utils.tools.JsonTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
//import com.shark.utils.PageVo;

@Slf4j
@Service
public class TradeService extends ServiceImpl<FuelOrderMapper, FuelOrderModel> {
    public PageVo listFuelOrder(FuelOrderCriteria criteria) {
        log.info("listFuelOrder criteria:{}", JSONUtil.toJsonStr(criteria));
        SignUtils.checkSign(criteria);
        ReqContext.AccessContext contextAccess = ReqContext.getContext();
        criteria.setGroupId(contextAccess.getGroupId());
        criteria.setStaList(contextAccess.getAllowStation());
//        LambdaQueryChainWrapper<FuelOrderModel> wrapper = this.lambdaQuery()
//                .eq(Objects.nonNull(criteria.getGroupId()), FuelOrderModel::getGroupId, criteria.getGroupId())
//                .in(Objects.nonNull(criteria.getStaList()), FuelOrderModel::getStationId, criteria.getStaList())
//                .eq(Objects.nonNull(criteria.getStationId()), FuelOrderModel::getStationId, criteria.getStationId())
//                .eq(Objects.nonNull(criteria.getNozNo()), FuelOrderModel::getNozNo, criteria.getNozNo())
//                .eq(Objects.nonNull(criteria.getPrId()), FuelOrderModel::getPrId, criteria.getPrId())
//                .eq(Objects.nonNull(criteria.getCompanyId()), FuelOrderModel::getCompanyId, criteria.getCompanyId())
//                .eq(Objects.nonNull(criteria.getMemberId()), FuelOrderModel::getMemberId, criteria.getMemberId())
//                .eq(FuelOrderModel::getInvalid, 0)
//                .eq(Objects.nonNull(criteria.getMemberCard()), FuelOrderModel::getMemberCard, criteria.getMemberCard())
//                .eq(Objects.nonNull(criteria.getShiftNo()), FuelOrderModel::getShiftNo, criteria.getShiftNo())
//                .eq(Objects.nonNull(criteria.getEmpId()), FuelOrderModel::getEmpId, criteria.getEmpId())
//                .eq(Objects.nonNull(criteria.getEmpCard()), FuelOrderModel::getEmpCard, criteria.getEmpCard())
//                .eq(Objects.nonNull(criteria.getPaidStatus()), FuelOrderModel::getPaidStatus, criteria.getPaidStatus())
//                .eq(Objects.nonNull(criteria.getPaidTypeId()), FuelOrderModel::getPaidTypeId, criteria.getPaidTypeId())
//                .eq(Objects.nonNull(criteria.getFuelOrderId()), FuelOrderModel::getFuelOrderId, criteria.getFuelOrderId())
//                .ge(Objects.nonNull(criteria.getBeginTime()), FuelOrderModel::getCreatedTime, criteria.getBeginTime())
//                .lt(Objects.nonNull(criteria.getEndTime()), FuelOrderModel::getCreatedTime, criteria.getEndTime())
//                if(criteria.getGroupId().equals("20237")) {
//                    .and(Wrapper -> Wrapper.or().in(criteria::getNozNo(),3,4,5,6,12,13,17,18,22,23).and().eq(MemberAnalysisModel::getPhoneNum, criteria.getKeyword()).or().eq(MemberAnalysisModel::getMemberName, criteria.getKeyword()));
//                }
//                .orderByAsc(FuelOrderModel::getCreatedTime);

//        IPage<FuelOrderModel> page = wrapper.page(new Page<>(criteria.getPageIndex(), criteria.getPageSize()));
        log.info("fuel queryPage:{}",JsonTool.toJSONString(criteria));
        List<FuelOrderModel> list = baseMapper.listPage(criteria, (criteria.getPageIndex() - 1) * criteria.getPageSize());
        TotalModel totalModel = baseMapper.getTotal(criteria);
        return PageVo.of(totalModel.getCnt(),criteria.getPageIndex(), criteria.getPageSize(),  list);
    }
}